
package cz.czu.pef.DataModelling.beans;

import java.util.List;
import java.util.logging.*;

import javax.annotation.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.czu.pef.DataModelling.domainObjects.Address;
import cz.czu.pef.DataModelling.ejb.api.AddressEjbInterface;
import cz.czu.pef.common.CustomFilterField;
import cz.czu.pef.common.CustomSortField;

@Component
@ViewScoped
@ManagedBean
@Scope(value="view")
public class AddressBean extends AbstractExtendedTableBean<Address>
{
	private static final long serialVersionUID = 6469829375417092825L;

	public static final Logger logger = Logger.getLogger(AddressBean.class.getName());

	private Address address;
	private AddressEjbInterface addressEjb;

	public AddressBean()
	{
		super();
	}

	@PostConstruct
	public void create()
	{
	  addressEjb = this.applicationContext.getBean(AddressEjbInterface.class);
	  address = new Address();
	}

	public String createNew()
	{
		try
		{
			logger.finest("createNew():: Entry");

			logger.info("createNew():: address=" + address);
			if (address != null)
			{
				// persist new person
			  addressEjb.merge(address);
			}

			// reset detail form's data
			address = this.applicationContext.getBean(Address.class);

			return null;
		}
		finally
		{
			logger.finest("createNew():: Exit");

		}
	}
	
	public Address getAddress()
	{
		return address;
	}

	public void setAddress(Address address)
	{
		this.address = address;
	}

  @Override
  public boolean canBeRemoved(Address typedObject)
  {
    return addressEjb.canBeRemoved(typedObject);
  }

  @Override
  public void removeById(long id)
  {
    addressEjb.removeById(id);
  }

  @Override
  public Address getByStringId(String myKey)
  {
    return addressEjb.getByStringId(myKey);
  }

  @Override
  public List<Address> findAllByRangeWithFilterAndSort(Integer firstRow, Integer endRow, List<CustomFilterField> filterFields, List<CustomSortField> sortFields)
  {
    return addressEjb.findAllByRangeWithFilterAndSort(firstRow, endRow, filterFields, sortFields);
  }

  @Override
  public Long countWithFilter(List<CustomFilterField> filterFields)
  {
    return addressEjb.countWithFilter(filterFields);
  }
}
